package exo1;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import Exceptions.*;

public class CompteEpargne extends CompteAvecLimite {
	
	private static final double minimumVersement  = 1000;
	private static final double tauxInteret       = 4.5;
	private final double minimumCredit;

	public CompteEpargne(String nom, int numero, double solde,
			double minimumCredit) throws SoldeMinimumRequis
	{
		super(nom, numero, solde);

		// Le solde a la creation doit etre au minimum egal au minimum de versement
		if(solde < CompteEpargne.minimumVersement){
			throw new SoldeMinimumRequis("Le solde initial doit etre au minimum de "+CompteEpargne.minimumVersement+" euros\n", solde);
		}
		this.minimumCredit = minimumCredit;
	}
	@Override
	public boolean crediterCompte(double montant)
	{
		try
		{
			if(  this.minimumCredit < montant )
					throw new CreditNonAutorise("Credit non autorisee, minimum sur les operations de credit : "+ this.minimumCredit, montant);
			this.setSolde(this.getSolde()+montant);
			return true;
		}
		catch(CreditNonAutorise e){
			System.out.println(e);
			return false;
		}
	}
	
	@Override
	public boolean debiterCompte(double montant)
	{
		//this. -= montant;
		return true;
	}
	public boolean calculerInteret(){
		// On recupere le numero du mois courant
		DateFormat dateFormat = new SimpleDateFormat("MM");
		Date date = new Date();
		int mois = Integer.parseInt(dateFormat.format(date));
		
		//Puis on calcul le taux d'interet pour ce mois
		double solde = this.getSolde();
		double txInteret = CompteEpargne.getTauxInteret()/100;
		double interets = ((solde * txInteret) /12) * mois;
		
		this.setSolde(this.getSolde()+ interets);
		return true;
	}
	public static double getTauxInteret(){
		return tauxInteret;
	}
	public double getMinimumCredit(){
		return this.minimumCredit;
	}
	@Override
	public String toString(){
		return super.toString();
	}
}
